# -*- coding: utf-8 -*-
# @Time    : 18-6-28 上午9:18
# @Author  : hp
# @Email   : shaoeric@foxmail.com

import pymysql
from .views import bp
from flask import g, session
from config import CMS_USER_ID

@bp.before_request
def before_request():
    # 打开数据库连接  配置项：连接、用户名、密码、数据库名 编码
    g.db = pymysql.connect("localhost", "root", "root", "bookSale", charset='utf8')
    # 使用 cursor() 方法创建一个游标对象 cursor
    g.cursor = g.db.cursor()
    if CMS_USER_ID in session:
        g.username = session.get(CMS_USER_ID)


@bp.teardown_request
def teardown_request(exception):
    g.cursor.close()
    g.db.close()